home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlibs.zip / STRNREV.C < prev    next >
Text File  |  1993-01-04  |  1KB  |  49 lines

  1.  
  2. /*  File   : strnrev.c
  3.     Author : Richard A. O'Keefe.
  4.     Updated: 1 June 1984
  5.     Defines: strnrev()
  6.  
  7.     char *strnrev(dst, src, len)
  8.  
  9.     copies len characters of src to dst, in REVERSE order.  dst will
  10.     be terminated by a NUL character.
  11.  
  12.     Returns a pointer to dst.
  13.  
  14.     Note: this function is perfectly happy to reverse a string into the
  15.     same place, strnrev(x, x, L) will work.
  16.  
  17.     It will not work for partially overlapping source and destination.
  18. */
  19.  
  20. #define  NUL   '\0'
  21.  
  22. char *
  23. strnrev(dsta, srca, len)
  24. register char *dsta, *srca;
  25.          int  len;
  26.     {
  27.         char *dstz, *srcz, *result;
  28.         char t;
  29.  
  30.         result = dsta;
  31.  
  32.         for (srcz = srca; --len >= 0 && *srcz; srcz++) ;
  33.         dstz = dsta+(srcz-srca);
  34.         /*
  35.         If srcz was stopped by len running out, it points just after
  36.         the last character of the source string, and it and dstz are
  37.         just right.  Otherwise, it was stopped by hitting NUL, and is
  38.         in the right place. dstz should get a NUL.
  39.         */
  40.         *dstz = NUL;
  41.         while (srcz > srca) {
  42.             t = *--srcz;
  43.             *--dstz = *srca++;
  44.             *dsta++ = t;
  45.         }
  46.         return result;
  47.     }
  48.  
  49.